package com.yvdelx.cyclowomen.utility;

import com.jjoe64.graphview.GraphView.GraphViewData;

import java.util.ArrayList;
import java.util.List;

public class MathUtility {

    /**
     * Compute the average of a float list
     * @param floatList List<Float>
     * @return Average
     */
    public static Float computeAverage(List<Float> floatList) {
        if(floatList.isEmpty())
            return 0f;
        Integer size = floatList.size();
        Float additive = 0f;
        for(Float fl : floatList){
            additive += fl;
        }
        return additive/size;
    }

    /**
     * Verify if datas contains a not null value
     * @param datas GraphViewData[]
     * @return true if average of x axis is 0
     */
    public static Boolean isAverageNull(GraphViewData[] datas){
        List<Float> values = new ArrayList<Float>();
        for(GraphViewData data : datas){
            if(data != null)
                values.add(toFloat(data.getX()));
            else
                values.add(0f);
        }
        Float average = computeAverage(values);
        return average == 0;
    }

    /**
     * Transform a double value in a float value
     * @param value Double
     * @return Float
     */
    public static Float toFloat(Double value){
        return value.floatValue();
    }

}
